package ws.jdbc.concurrent.driver.sqlserver.tds;

import java.nio.ByteBuffer;

/**
 * Handles the {@code DONEPROC} token reading process.
 * 
 * <p>
 * Indicates the completion status of commands within a stored procedure. 
 * This is also generated for EXEC statements at the ad hoc T-SQL execution level in a language event.
 * </p>
 *
 * <p>
 * A DONEPROC token is sent when all the statements within a stored procedure have been executed.
 * </p>
 *
 * <p>
 * A DONEPROC token may be followed by another DONEPROC token or a DONEINPROC only if the DONE_MORE bit is set in the Status value.
 * </p>
 *
 * <p>
 * There is a separate DONEPROC token sent for each stored procedure called.
 * </p>
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class DoneProcTokenReader extends DoneTokenReader {

  /** Status flag: This DONEPROC message is associated with an RPC within a set of batched RPCs. This flag is not set on the last RPC in the batch). */
  private static final short maskDoneRPCInBatch = 0x80;

  /**
   * Create a new reader instance.
   *
   * @param stream data input stream to read the token from.
   */
  public DoneProcTokenReader(final ByteBuffer stream) {
    super(stream);
  }

  /**
   * Check if status is DONE_RPCINBATCH.
   *
   * @return {@code true} if it is, otherwise {@code false}.
   */
  boolean isDoneRPCInBatch() {
    return (status & maskDoneRPCInBatch) == maskDoneRPCInBatch;
  }

  /**
   * Token as a {@code String}.
   *
   * @return token fields as a {@code String}.
   */
  @Override
  public String toString() {
    return done2String("DoneProcTokenReader", "DONE_FINAL=" + isDoneFinal()
            + ", DONE_MORE=" + isDoneMore()
            + ", DONE_ERROR=" + isDoneError()
            + ", DONE_INXACT=" + isDoneInxact()
            + ", DONE_COUNT=" + isDoneCount()
            + ", DONE_RPCINBATCH=" + isDoneRPCInBatch()
            + ", DONE_SVRERROR=" + isDoneSrvError());
  }
}
